import Vue from 'vue'
import VueRouter from 'vue-router'

// import Home from '../views/Home.vue'
const Home = () => import(/* webpackChunkName: "Home-LetIn-Welcome" */ '../views/Home.vue')
// import LetIn from '../views/Let_in.vue'
const LetIn = () => import(/* webpackChunkName: "Home-LetIn-Welcome" */ '../views/Let_in.vue')
// import Welcome from '../views/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "Home-LetIn-Welcome" */ '../views/Welcome.vue')

// import User from '../views/User.vue'
const User = () => import(/* webpackChunkName: "User-Power-Roles" */ '../views/User.vue')
// import Power from '../views/Power.vue'
const Power = () => import(/* webpackChunkName: "User-Power-Roles" */ '../views/Power.vue')
// import Roles from '../views/Roles.vue'
const Roles = () => import(/* webpackChunkName: "User-Power-Roles" */ '../views/Roles.vue')

// import Cates from '../views/Cate.vue'
const Cates = () => import(/* webpackChunkName: "Cates-Params" */ '../views/Cate.vue')
// import Params from '../views/Params.vue'
const Params = () => import(/* webpackChunkName: "Cates-Params" */ '../views/Params.vue')

// import List from '../views/Goods_list.vue'
const List  = () => import(/* webpackChunkName: "List-GoodsAdd" */ '../views/Goods_list.vue')
// import GoodsAdd from '../views/Goods_add.vue'
const GoodsAdd = () => import(/* webpackChunkName: "List-GoodsAdd" */ '../views/Goods_add.vue')

// import Order from '../views/Order.vue'
const Order = () => import(/* webpackChunkName: "Order-Report" */ '../views/Order.vue')
// import Report from '../views/Report.vue'
const Report = () => import(/* webpackChunkName: "Order-Report" */ '../views/Report.vue')


Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: Welcome
    },
    {
      path: '/users',
      component: User
    },
    {
      path: '/rights',
      component: Power
    },
    {
      path: '/roles',
      component: Roles
    },
    {
      path: '/categories',
      component: Cates
    },
    {
      path: '/params',
      component: Params
    },
    {
      path: '/goods',
      component: List,
    },
    {
      path: '/goods/add',
      component: GoodsAdd
    },
    {
      path: '/orders',
      component: Order
    },
    {
      path: '/reports',
      component: Report
    }
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/login',
    name: 'LetIn',
    component: LetIn
  }
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 从哪个路径跳转而来
  // next 是一个函数，表示放放行
  // next() 放行    next('/login')   强制跳转

  if (to.path === '/login') {
    next()
  } else {
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) {
      return next('/login')
    } else {
      next()
    }
  }


})

export default router
